package com.venus.plan.planresource.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.venus.common.action.BaseAction;
import com.venus.common.utils.ConditionUtil;
import com.venus.common.utils.Constants;
import com.venus.common.utils.DateUtil;
import com.venus.component.page.PageInfo;
import com.venus.model.PlanResourceVO;
import com.venus.plan.planresource.form.PlanResourceForm;
import com.venus.plan.planresource.service.IPlanResourceService;

public class PlanResourceMultiOperateAction extends BaseAction {
	
	private IPlanResourceService planResourceService;
	
	public void setPlanResourceService(IPlanResourceService planResourceService){this.planResourceService = planResourceService;}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		PlanResourceForm objPlanResourceForm = (PlanResourceForm)form;
		String strMethod = objPlanResourceForm.getMethod();
		log.info("调用方法:"+strMethod);
		if("list".equals(strMethod)){
			return this.list(mapping, objPlanResourceForm, request, response);
		}else if("create".equals(strMethod)){
			return this.create(mapping, objPlanResourceForm, request, response);
		}else if("insert".equals(strMethod)){
			return this.insert(mapping, objPlanResourceForm, request, response);
		}else if("edit".equals(strMethod)){
			return this.edit(mapping, objPlanResourceForm, request, response);
		}else if("update".equals(strMethod)){
			return this.update(mapping, objPlanResourceForm, request, response);
		}else if("delete".equals(strMethod)){
			return this.delete(mapping, objPlanResourceForm, request, response);
		}
		return super.execute(mapping, form, request, response);
	}
	
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		this.saveToken(request);
		PlanResourceForm objPlanResourceForm = (PlanResourceForm)form;
		List<ConditionUtil> lstConditionUtils = new ArrayList<ConditionUtil>();
		// 只显示当前用户的数据
		lstConditionUtils.add(new ConditionUtil("and", "pubUser.userId", "=", this.getLoginUser(request).getUserId()+"", false));
		// 按日期倒序
		lstConditionUtils.add(new ConditionUtil("order by", "createDate", "", "desc", false));
		// 得到分页信息
		PageInfo objPageInfo = this.planResourceService.getPageInfo(objPlanResourceForm.getPageRecord(), objPlanResourceForm.getCurPage(), lstConditionUtils);
		request.setAttribute("objPageInfo", objPageInfo);
		request.setAttribute("lstPlanResourceVOs", objPageInfo.getLstRecords());
		return mapping.findForward("list");
	}
	
	public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		this.saveToken(request);
		
		return mapping.findForward("create");
	}
	
	public ActionForward insert(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(!this.isTokenValid(request)){
			return this.list(mapping, form, request, response);
		}
		this.resetToken(request);
		PlanResourceForm objPlanResourceForm = (PlanResourceForm)form;
		// 新实例
		PlanResourceVO objPlanResourceVO = new PlanResourceVO();
		objPlanResourceVO.setCreateDate(new Date());
		objPlanResourceVO.setDoneActionCount(0);
		objPlanResourceVO.setEndDate(DateUtil.parseDate(objPlanResourceForm.getEndDate(), "yyyy-MM-dd"));
		objPlanResourceVO.setStatus(Constants.PLAN_STATUS_NEW);
		objPlanResourceVO.setNote(objPlanResourceForm.getNote());
		objPlanResourceVO.setPriority(objPlanResourceForm.getPriority());
		objPlanResourceVO.setProgress(0);
		objPlanResourceVO.setPubUser(this.getLoginUser(request));
		objPlanResourceVO.setStartDate(DateUtil.parseDate(objPlanResourceForm.getStartDate(), "yyyy-MM-dd"));
		objPlanResourceVO.setResourceType(objPlanResourceForm.getResourceType());
		objPlanResourceVO.setProperty(objPlanResourceForm.getProperty());
		objPlanResourceVO.setPlanResourceName(objPlanResourceForm.getPlanResourceName());
		// 保存实例
		this.planResourceService.savePlanResourceVO(objPlanResourceVO);
		// 实例ID返回到页面
		request.setAttribute("newPlanResourceId", objPlanResourceVO.getPlanResourceId());
		// 跳转到list页面
		return this.list(mapping, objPlanResourceForm, request, response);
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		this.saveToken(request);
		PlanResourceForm objPlanResourceForm = (PlanResourceForm)form;
		PlanResourceVO objPlanResourceVO = this.planResourceService.loadPlanResourceVO(objPlanResourceForm.getPlanResourceId());
		request.setAttribute("objPlanResourceVO", objPlanResourceVO);
		return mapping.findForward("edit");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(!this.isTokenValid(request)){
			return this.list(mapping, form, request, response);
		}
		this.resetToken(request);
		PlanResourceForm objPlanResourceForm = (PlanResourceForm)form;
		// 加载PlanResourceVO实例
		PlanResourceVO objPlanResourceVO = this.planResourceService.loadPlanResourceVO(objPlanResourceForm.getPlanResourceId());
		objPlanResourceVO.setEndDate(DateUtil.parseDate(objPlanResourceForm.getEndDate(), "yyyy-MM-dd"));
		objPlanResourceVO.setStatus(Constants.PLAN_STATUS_NEW);
		objPlanResourceVO.setNote(objPlanResourceForm.getNote());
		objPlanResourceVO.setPriority(objPlanResourceForm.getPriority());
		objPlanResourceVO.setStartDate(DateUtil.parseDate(objPlanResourceForm.getStartDate(), "yyyy-MM-dd"));
		objPlanResourceVO.setResourceType(objPlanResourceForm.getResourceType());
		objPlanResourceVO.setProperty(objPlanResourceForm.getProperty());
		objPlanResourceVO.setPlanResourceName(objPlanResourceForm.getPlanResourceName());
		// 修改PlanResourceVO实例
		this.planResourceService.saveOrUpdatePlanResourceVO(objPlanResourceVO);
		// 修改的实例ID返回页面
		request.setAttribute("updatePlanResourceId", objPlanResourceVO.getPlanResourceId());
		// 返回list页面
		return this.list(mapping, objPlanResourceForm, request, response);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(!this.isTokenValid(request)){
			return this.list(mapping, form, request, response);
		}
		this.resetToken(request);
		PlanResourceForm objPlanResourceForm = (PlanResourceForm)form;
		// 删除页面选择的多个PlanResource实例
		this.planResourceService.deletePlanResourceVO(objPlanResourceForm.getPlanResourceIds());
		return this.list(mapping, form, request, response);
	}
}
